/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jprojetopharmalink;


import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.spi.DirStateFactory.Result;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrador
 */
public class Update {

    public static Connection con;

    public void setUpdate(String sql) throws IOException {
        con = Conexao.getConnetion();
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            if (JOptionPane.showConfirmDialog(null, " Confirma a gravação? ", " Pergunta do Sistema ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                pst.executeUpdate();
                JOptionPane.showMessageDialog(null, "Sucesso!");
            } else {
                con.close();
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            try {
                con.close();
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1);
                Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }
    public void setDelete(String sql) throws IOException {
        con = Conexao.getConnetion();
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            if (JOptionPane.showConfirmDialog(null, " Confirma a exclusão? ", " Pergunta do Sistema ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                pst.executeUpdate();
                JOptionPane.showMessageDialog(null, "Sucesso!");
            } else {
                con.close();
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            try {
                con.close();
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1);
                Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }
    public void setUpdateControle(String sql) throws IOException {
        con = Conexao.getConnetion();
        try {
            PreparedStatement pst = con.prepareStatement(sql);
            
                pst.executeUpdate();
      
                con.close();
  

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            try {
                con.close();
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1);
                Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }
        
        public String getId(String campo) throws IOException {
        
            con = Conexao.getConnetion();
            
        try {
            ResultSet rsId = null;
            Statement stId = null;
            
            stId = con.createStatement();
            
            rsId = stId.executeQuery("select valor from controle where campo = '"+campo+"'");
                        
            while(rsId.next()){
                return rsId.getString(1);
            }
            
            con.close();
    

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex+" setconferencia");
            try {
                con.close();
            } catch (SQLException ex1) {
                JOptionPane.showMessageDialog(null, ex1+"setconferencia");
                Logger.getLogger(Update.class.getName()).log(Level.SEVERE, null, ex1+" setconferencia");
            }
        }
        return null;
    }
      
}
